/// <reference path="../../../Resources/js/jquery/jquery.debug.js" />
/// <reference path="../../Additions/Addition.js" />
/// <reference path="../../ExtenderDuplicator.debug.js" />
/// <reference path="../../Items/MItem.js" />
/// <reference name="MicrosoftAjax.debug.js" />

Type.registerNamespace("BM");

// TabbedDisplayAddition object

BM.TabbedDisplayAddition = function(item, data) {
    BM.TabbedDisplayAddition.initializeBase(this, [item, data]);
    item.addFocused(Function.createDelegate(this, this.raiseFocused));
}

BM.TabbedDisplayAddition.prototype = {
    load: function () {
        BM.TabbedDisplayAddition.callBaseMethod(this, 'load');
        this.ensureTabExists();
        $(this.getTabNode()).click(Function.createDelegate(this, this.tabClicked));
        this.getItem().addDescriptionChanged(Function.createDelegate(this, this.descriptionChanged));
        this.raiseTabCreated();
    },

    descriptionChanged: function () {
        var tab = this.getTab();
        tab.setText(this.getItem().getDescription());
    },

    ensureTabExists: function () {
        this.createOrGetTabContainer();

        if (!this.getTabNode()) {
            var tab = this.addTab();
        }
    },

    tabClicked: function (event) {
        event.stopPropagation();
        this.getTab().choose();
        this.getItem().click();
    },

    raiseFocused: function () {
        /// <summary>
        /// when 'focused' event is raised on corresponding item
        /// the tab should be selected
        /// </summary>
        this.getTab().choose();
    },

    createOrGetTabContainer: function () {
        /// <summary>
        /// gets the existing tab container, or creates a new one if this one was not initiated 
        /// (e.g. when a MItem with tab container was just added)
        /// </summary>
        var tabContainer = $find(this.getTabContainerId());
        if (tabContainer == null) {
            var tabContainerNode = $get(this.getTabContainerId());
            if (tabContainerNode == null) {
                this.createTabContainerNode();
            }
            tabContainer = this.createTabContainer();
        }
        return tabContainer;
    },

    createTabContainerNode: function () {
        var div = document.createElement('div');
        div.setAttribute('id', this.getTabContainerId());
        div.setAttribute('class', BM.Tab.TabContainer);
        $(div).insertBefore($(this.getItem().getNode()).siblings()[0]);
        return div;
    },

    createTabContainer: function () {
        BM.ExtenderDuplicator.run(this.getTabContainerPrototypeId(), this.getTabContainerId());
        return this.getTabContainer();
    },

    getTabContainer: function () {
        return $find(this.getTabContainerId());
    },

    addTab: function () {
        var content = this.item.getDescription();
        content += BM.TabbedDisplayAddition.getLinkText("MForm-Btn MForm-CloseBtn", "BM.TabbedDisplayAddition.remove(this);return false;");
        var tab = this.getTabContainer().add(this.item.getId(), null, content);
        this.raiseTabCreated();
        return tab;
    },

    raiseTabCreated: function () {
        if (this.getClientTabCreated()) {
            var tabCreatedFunction = eval(this.getClientTabCreated());
            if (tabCreatedFunction != null) {
                tabCreatedFunction(this, { 'tab': this.getTab() });
            }
        }
    },

    unload: function () {
        BM.TabbedDisplayAddition.callBaseMethod(this, 'unload');
        var tab = this.getTab();
        if (tab != null) {
            tab.dispose();
        }
    },

    getTabId: function () {
        return this.item.getId() + "__" + BM.Tab.Tab;
    },

    getTabNode: function () {
        var tc = this.getTabContainer();
        if (!tc) return null;
        return tc.getTab(this.getTabId());
    },

    getTab: function () {
        var tab = this.getTabNode();
        if (tab == null) return null;
        return BM.Tab.get(tab);
    },

    getTabContainerSuffix: function () {
        return this.getProperty('TCRef') || 'default';
    },

    getClientTabCreated: function () {
        return this.getProperty('clientTabCreated');
    },

    getTabContainerId: function () {
        return this.item.getParentItem().getId() + '_' + this.getTabContainerSuffix();
    },

    getTabContainerPrototypeId: function () {
        return this.item.getParentItem().basis.node.id + '_' + this.getTabContainerSuffix();
    }

}

BM.TabbedDisplayAddition.registerClass("BM.TabbedDisplayAddition", BM.Addition);

BM.TabbedDisplayAddition.remove = function(removalButton) {
    if (confirm(BM.Messages["ReallyDelete"])) {
        var tab = removalButton.parentNode.parentNode.parentNode;
        var tabItem = BM.Tab.get(tab);
        var item = MItem.getById(tabItem.getItemId());
        item.remove();
    }
}

BM.TabbedDisplayAddition.getLinkText = function(className, onClick) {
    var builder = new Sys.StringBuilder("<a href=\"#\" ");
    builder.append("\" class=\"");
    builder.append(className);
    builder.append("\"");    
    if (onClick) {
        builder.append(" onclick=\"");
        builder.append(onClick);
        builder.append("\" ");
    }
    builder.append("></a>");
    return builder.toString();
}

